package com.suixingpay.seckill.exception;

import com.suixingpay.seckill.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description 异常捕获处理
 * @Author zhu_jinsheng[zhu_js@suixingpay.com]
 * @Date 2019/12/8 22:43
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class ExceptionHandle {
    /**
     * @Description 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
     * @Author zhu_jinsheng[zhu_js@suixingpay.com]
     * @Param e:  异常
     * @return: com.suixingpay.seckill.pojo.Result
     * @Date 2019/12/9 10:54
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionGet(Exception e){
        Result result = new Result();
        //将已知异常封装返回
        if(e instanceof DescribeException){
            DescribeException MyException = (DescribeException) e;
            result.set(MyException.getStatus(), MyException.getMessage(), null);
            return result;
        }
        //未知错误，写入日志并封装返回信息
        log.error("【系统异常】", e);
        ExceptionEnum exceptionEnum = ExceptionEnum.SYS_ERROR;
        result.set(exceptionEnum.getStatus(), exceptionEnum.getMsg(), null);
        return result;
    }
}
